<template>
  <view class="x-send-coupon">
    <send-coupon
      v-if="coupon"
      :sign="coupon.sign"
      :send_coupon_params="coupon.send_coupon_params"
      :send_coupon_merchant="coupon.send_coupon_merchant"
      :suggest_immediate_use="suggestImmediateUse"
      @sendcoupon="handleCustomEvent"
      @userconfirm="handleUserConfirm"
      @customevent="handleCustomEvent"
    >
      <slot />
    </send-coupon>
    <slot v-else />
  </view>
</template>

<script>
export default {
  name: 'XSendCoupon',
  props: {
    coupon: {
      type: Object,
      default: () => null
    },
    suggestImmediateUse: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {};
  },
  created() {},
  methods: {
    handleCustomEvent({ detail }) {
      this.$store.dispatch('product/setCouponSend', detail).then(() => {
        this.handleConfirm();
      }).catch(() => {
        this.handleConfirm();
      });
      this.$XTrack('HandleSendCouponConfirm', detail);
    },
    handleConfirm() {
      this.$emit('sendcoupon');
    },
    handleUserConfirm() {
      this.$emit('userconfirm');
    }
  }
};
</script>

<style>

</style>
